using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimeSheet.Logger;

namespace TimeSheet.Protected
{
    /// <summary>
    /// Summary description for Login.
    /// </summary>
    public class Login : Page
    {
        protected Label lblSiteName;
        protected TextBox txtUser;
        protected TextBox txtPassword;
        protected Button btnLogin;

        private void Page_Load(object sender, EventArgs e)
        {
            lblSiteName.Text = AppSettings.SiteName;
            Session[AppSettings.AdministratorSessionName] = null;
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion

        private void btnLogin_Click(object sender, EventArgs e)
        {
            TimeSheetBLL.Administrator admin = new TimeSheetBLL.Administrator();
            admin.AdminName = txtUser.Text;
            admin.Password = txtPassword.Text;
            bool authenticated = false;

            try
            {
                authenticated = admin.Authenticate();
            }
            catch (Exception ex)
            {
                ExceptionLogger.AddException(ex);
            }

            if (authenticated)
            {
                Session[AppSettings.AdministratorSessionName] = admin;
                Response.Redirect("admin.aspx");
            }
        }
    }
}